
# Setup ------------------------------------------------------------------------
  # Options
  options(stringsAsFactors = F)
  # Packages
  library(pacman)
  p_load(data.table, magrittr, lubridate, ggplot2, extrafont, Cairo, rmapshaper)
  # Directories
  dir_save <- "S:/Edward/Elasticity/DataR/Results/Results20170924/"
  dir_project <- "/Users/edwardarubin/Dropbox/Research/MyProjects/NaturalGas/"
  dir_rds <- paste0(dir_project, "DataR/")
  dir_fig <- paste0(dir_project, "Presentations/Beamer20170926HEREW/Figures/")
  # Define colors
  dark_grey    <- "#7D7D7D"
  light_grey   <- "#C7C7C7"
  v_light_grey <- "#EEEEEE"
  mid_grey     <- "#A2A2A2"
  purple       <- "#6A1B9A"
  red_pink     <- "#E91E63"
  orange       <- "#FFA000"
  # My ggplot themes
  theme_beamer <- theme_bw() + theme(
    panel.border = element_blank(),
    axis.ticks = element_blank(),
    legend.position = 'bottom',
    legend.key = element_blank(),
    panel.grid = element_blank(),
    text = element_text(color = "black", size = 16),
    title = element_text(size = 18),
    legend.text = element_text(size = 18),
    legend.key.size = unit(2.5, "cm"))

# Add variables (EI server) ----------------------------------------------------
# NOTE: Assumes data already loaded
  bill_dt[, amt_excess := thm - allowance]
  sub_dt[, amt_excess := thm - allowance]

# Plot densities for excess therms (EI server) ---------------------------------
  gg1 <- ggplot(data = bill_dt[, .(season, amt_excess)],
    aes(x = amt_excess, fill = season)) +
    geom_density(alpha = 0.9, color = NA) +
    geom_vline(xintercept = 0, linetype = 2, color = dark_grey) +
    geom_hline(yintercept = 0, linetype = 1) +
    xlab("Therms exceeding allowance") +
    ylab("") +
    scale_fill_manual("", labels = c("Summer", "Winter"), values = c(red_pink, light_grey)) +
    theme_beamer +
    theme(axis.text.y = element_blank())
  gg2 <- ggplot(data = bill_dt[season == "winter", .(care, amt_excess)],
    aes(x = amt_excess, fill = care)) +
    geom_density(alpha = 0.9, color = NA) +
    geom_vline(xintercept = 0, linetype = 2, color = dark_grey) +
    geom_hline(yintercept = 0, linetype = 1) +
    xlab("Therms exceeding allowance") +
    ylab("") +
    scale_fill_manual("", labels = c("Non-CARE", "CARE"), values = c(purple, light_grey)) +
    theme_beamer +
    theme(axis.text.y = element_blank())
  gg3 <- ggplot(data = bill_dt[season == "summer", .(care, amt_excess)],
    aes(x = amt_excess, fill = care)) +
    geom_density(alpha = 0.9, color = NA) +
    geom_vline(xintercept = 0, linetype = 2, color = dark_grey) +
    geom_hline(yintercept = 0, linetype = 1) +
    xlab("Therms exceeding allowance") +
    ylab("") +
    xlim(c(-50,125)) +
    scale_fill_manual("", labels = c("Non-CARE", "CARE"), values = c(purple, light_grey)) +
    theme_beamer +
    theme(axis.text.y = element_blank())

# Save (EI server) -------------------------------------------------------------
  saveRDS(
    object = list(gg1, gg2, gg3),
    file = paste0(dir_save, "excessThermDensities.rds")
    )

# Load (my computer) -----------------------------------------------------------
  # Load the list of plots
  gg_list <- readRDS(paste0(dir_rds, "excessThermDensities.rds"))

# Save figures with proper fonts (my computer) ---------------------------------
  ggsave(
    filename = "excessSeasonal.pdf",
    path = dir_fig,
    plot = gg_list[[1]] +
      theme(
        text = element_text(family = "Roboto", color = "black", size = 16),
        legend.key.size = unit(1, "cm")),
    width = 16 * 0.7,
    height = 10 * 0.7,
    device = cairo_pdf)
  embed_fonts(paste0(dir_fig, "excessSeasonal.pdf"))
  ggsave(
    filename = "excessCareWinter.pdf",
    path = dir_fig,
    plot = gg_list[[2]] +
      theme(
        text = element_text(family = "Roboto", color = "black", size = 16),
        legend.key.size = unit(1, "cm")),
    width = 16 * 0.7,
    height = 10 * 0.7,
    device = cairo_pdf)
  embed_fonts(paste0(dir_fig, "excessCareWinter.pdf"))
  ggsave(
    filename = "excessCareSummer.pdf",
    path = dir_fig,
    plot = gg_list[[3]] +
      theme(
        text = element_text(family = "Roboto", color = "black", size = 16),
        legend.key.size = unit(1, "cm")),
    width = 16 * 0.7,
    height = 10 * 0.7,
    device = cairo_pdf)
  embed_fonts(paste0(dir_fig, "excessCareSummer.pdf"))
